package com.terry.test2.new2.impl;

import com.terry.test2.dp.Account;
import com.terry.test2.dp.AccountId;
import com.terry.test2.dp.AccountNumber;
import com.terry.test2.dp.UserId;
import com.terry.test2.new2.AccountBuilder;
import com.terry.test2.new2.AccountRepository;
import com.terry.test2.old.AccountDO;
import com.terry.test2.old.AccountMapper;

/**
 * 防腐层防止 ORM层以及库表数据变化
 *
 * @author 小八
 * @date 2023年02月01日15:30
 */
public class AccountRepositoryImpl implements AccountRepository {

    private AccountMapper accountMapper;

    private AccountBuilder accountBuilder;

    @Override
    public Account find(AccountId id) {
        AccountDO accountDO = accountMapper.selectById(id.getAccountId());
        return accountBuilder.toAccount(accountDO);
    }

    @Override
    public Account find(AccountNumber accountNumber) {
        AccountDO accountDO = accountMapper.selectByAccountNumber(accountNumber.getAccountNumber());
        return accountBuilder.toAccount(accountDO);
    }

    @Override
    public Account find(UserId userId) {
        AccountDO accountDO = accountMapper.selectByUserId(userId.getUserId());
        return accountBuilder.toAccount(accountDO);
    }

    @Override
    public Account save(Account account) {
        AccountDO accountDO = accountBuilder.fromAccount(account);
        if(accountDO.getId() == null){
            accountMapper.save(accountDO);
        }else{
            accountMapper.update(accountDO);
        }
        return accountBuilder.toAccount(accountDO);
    }

}
